package com.kaili.basic.service.impl;


import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.AlarmMapper;
import com.kaili.basic.pojo.dto.MdmAlarmPageListDto;
import com.kaili.basic.pojo.entity.MdmAlarmEntity;
import com.kaili.basic.pojo.vo.MdmAlarmEmailVo;
import com.kaili.basic.pojo.vo.MdmAlarmPageListVo;
import com.kaili.basic.pojo.vo.MdmAlarmPhoneVo;
import com.kaili.basic.service.IMdmAlarmService;
import com.kaili.common.util.Http;
import com.kaili.common.util.MailUtil;
import org.springframework.stereotype.Service;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @Desc:
 * @author: jianfeng
 * @Time: 2019/8/2 15:33
 * @Copyright: © 2018 杭州凯立通信有限公司 版权所有
 * @Warning: 本内容仅限于公司内部传阅, 禁止外泄或用于其它商业目的
 */
@Service
public class AlarmServiceImpl extends ServiceImpl<AlarmMapper, MdmAlarmEntity> implements IMdmAlarmService {

    /**
    * @Desc:  分页查询告警信息
    * @author: jianfeng
    * @Time: 2019/8/2 15:35
    * @param page
    * @param alarmPageListDto
    * @return com.baomidou.mybatisplus.plugins.Page<com.kaili.basic.pojo.vo.MdmAlarmPageListVo>
    */
    @Override
    public Page<MdmAlarmPageListVo> queryPageList(Page<MdmAlarmPageListVo> page, MdmAlarmPageListDto alarmPageListDto) {
        return page.setRecords(baseMapper.queryPageList(page,alarmPageListDto));
    }

    /**
    * @Desc:  导出
    * @author: jianfeng
    * @Time: 2019/8/2 15:48
    * @param alarmPageListDto
    * @return java.util.List<com.kaili.basic.pojo.vo.MdmAlarmPageListVo>
    */
    @Override
    public List<MdmAlarmPageListVo> export(MdmAlarmPageListDto alarmPageListDto) {
        return baseMapper.queryPageList(alarmPageListDto);
    }

    /**
    * @Desc:  告警信息发送邮件
    * @author: jianfeng
    * @Time: 2019/8/9 11:30
    *
    * @return void
    */
    //@Scheduled(cron="0 0/5 * * * ? ")//每5分钟执行一次
    @Override
    public void sendEmail() throws Exception {
        List<MdmAlarmEmailVo> list=baseMapper.queryEmailList();
        Map<String,String> map=new HashMap<String,String>();
        Integer[] integers = new Integer[list.size()];
        List<String> lis=new ArrayList<>();
        if(list.size()>0){
            map.put("subject","设备告警");
            for (MdmAlarmEmailVo data:list){
                Integer id=data.getId();
                integers[list.indexOf(data)] = id;
                lis.add("设备IMEI："+data.getImei());
                lis.add("设备SN："+data.getSn());
                lis.add("型号："+data.getModel());
                lis.add("告警内容："+data.getContent());
                lis.add("所属告警项："+data.getAlarmName());
                lis.add("告警时间："+data.getCreateTime()+"<br>");
                map.put("content",lis.toString());
                map.put("emailAddress",data.getEmail());
            }
            MailUtil.sendEmail(map);
            this.baseMapper.batchUpdateEmailByIds(integers);
        }
    }

    /**
    * @Desc:  告警信息发送短信
    * @author: jianfeng
    * @Time: 2019/8/9 11:30
    *
    * @return void
    */
    //@Scheduled(cron="0 0/5 * * * ? ")//每5分钟执行一次
    @Override
    public void sendSMS() {
        List<MdmAlarmPhoneVo> list=baseMapper.queryPhoneList();
        Integer[] integers = new Integer[list.size()];
        List<String> lis=new ArrayList<>();
        String phone="";
        if (list.size()>0){
            for (MdmAlarmPhoneVo data:list){
                Integer id=data.getId();
                integers[list.indexOf(data)] = id;
                phone=data.getPhone();
                lis.add("设备IMEI："+data.getImei());
                lis.add("设备SN："+data.getSn());
                lis.add("型号："+data.getModel());
                lis.add("告警内容："+data.getContent());
                lis.add("所属告警项："+data.getAlarmName());
                lis.add("告警时间："+data.getCreateTime()+"\r\n");
            }
            phone=phone.replace(";",",");
            //设备告警信息
            String data = lis.toString();
            //请求地址
            String uri = "http://userinterface.vcomcn.com/Opration.aspx";
            //发送告警信息
            String pstContent = createSubmitXml(phone,data);
            //接收状态值
            String state = Http.post(uri, pstContent);
            if (state.equals("00")) {
                //System.out.println("提交成功");
                this.baseMapper.batchUpdatePhoneByIds(integers);
            } else if (state.equals("01")) {
                System.out.println("帐号或密码错误");
                // return "01";
            } else if (state.equals("02")) {
                System.out.println("帐号欠费");
                // return "02";
            } else if (state.equals("09")) {
                System.out.println("无效的接收号码");
                // return "09";
            } else if (state.equals("10")) {
                System.out.println("网络或系统内部错误");
                //return "10";
            }

        }


    }

    /**
    * @Desc:  短信格式
    * @author: jianfeng
    * @Time: 2019/8/9 14:54
    * @param phone
    * @param data
    * @return java.lang.String
    */
    public static String createSubmitXml(String phone,String data) {
        StringBuffer sp = new StringBuffer();
        sp.append(String
                .format("<Group Login_Name=\"%s\" Login_Pwd=\"%s\" InterFaceID=\"0\" OpKind=\"51\">",
                        "hzkltxgs", MD5("sqr150610")));
        sp.append("<E_Time></E_Time>");
        sp.append("<Mobile>"+phone+"</Mobile>");
        sp.append("<Content><![CDATA[您好,设备告警信息:" + data + "]]></Content>");
        sp.append("<ClientID>"+UUID.randomUUID().toString()+"</ClientID>");
        sp.append("</Group>");
        return sp.toString();
    }

    /**
    * @Desc:  MD5加密
    * @author: jianfeng
    * @Time: 2019/8/9 14:26
    * @param encryptContent
    * @return java.lang.String
    */
    public static String MD5(String encryptContent) {
        String result = "";
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(encryptContent.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }

            result = buf.toString().toUpperCase();

        } catch (NoSuchAlgorithmException e) {

        }
        return result;
    }



}
